﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Search.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Search
open Microsoft.SharePoint.Client.Search.Query
open Microsoft.SharePoint.Client.Sharing
open Microsoft.SharePoint.Client.Utilities
open Microsoft.SharePoint.Client.WebParts
open Microsoft.SharePoint.Client.Application
open System.Security;

// =======================================================
// Modify search settings & query data
// =======================================================

let web = Client.Context.Web

Client.Load web

Client.Load web.AllProperties
// Out of the box
web.AllProperties.["SRCH_SB_SET_SITE"] <- "{\"Inherit\":true,\"ResultsPageAddress\":null,\"ShowNavigation\":false}"

// Redirect to search center
web.AllProperties.["SRCH_SB_SET_SITE"] <- "{\"Inherit\":false,\"ResultsPageAddress\":\"https://search.com/Pages/AllResults.aspx\",\"ShowNavigation\":false}"
web.Update()
Client.ExecuteQuery()

// Query SharePoint data
let query = new KeywordQuery(Client.Context)
query.QueryText <- "Path:https://geek.sharepoint.com/ AND ContentClass:STS_Site"
query.RowLimit <- 500;//max row limit is 500 for KeywordQuery
query.EnableStemming <- true
query.TrimDuplicates <- false

let searchExecutor = new SearchExecutor(Client.Context)
let results = searchExecutor.ExecuteQuery(query);
Client.ExecuteQuery();

results.Value.[0].RowCount

for resultRow in results.Value.[0].ResultRows do
    for key in resultRow.Keys do
        let mutable msg1 = key 
        if resultRow.[key] <> null then
            msg1 <- msg1 + " " + resultRow.[key].ToString()
        printfn "%s\n" msg1
    let msg = resultRow.["Title"].ToString() + ": " + resultRow.["Path"].ToString()
    printfn "%s\n" msg